<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
class SanPhamViewList extends JView{
	function display($tpl = null){
		global $mainframe, $option;
		$db		=& JFactory::getDBO();
		$uri	=& JFactory::getURI();
		$filter_frontpage	= $mainframe->getUserStateFromRequest( $option.'filter_frontpage',	'filter_frontpage',	'',				'int' );
		$filter_state		= $mainframe->getUserStateFromRequest( $option.'filter_state',		'filter_state',		'',				'word' );
		//$filter_parent		= $mainframe->getUserStateFromRequest( $option.'filter_parent',		'filter_parent',	0,				'int' );
		$filter_parent		= $mainframe->getUserStateFromRequest( $option.'parent',		'parent',	0,				'int' );
		$filter_order 		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		's.ordering','cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.'search',			'search',			'',				'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);
		// sanitize $filter_order
		if (!in_array($filter_order, array('s.name','s.parent','s.frontpage', 's.published', 's.ordering', 's.hit', 's.id'))) {
			$filter_order = 's.ordering';
		}

		if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
			$filter_order_Dir = '';
		}
		// Get data from the model
		$items		= & $this->get( 'Data');
		$total		= & $this->get( 'Total');
		$pagination = & $this->get( 'Pagination' );	
		$javascript 	= 'onchange="document.adminForm.submit();"';	
		
		$temp = $this->getListCategory('0','');
		//$lists['parent']= JHTML::_('select.genericList',$sel,'filter_parent', $javascript,'id','title',$filter_parent);]
		$caid=JRequest::getVar('parent');		
		$lists['parent'] = $this->createSel($temp,$filter_parent);
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['order'] = $filter_order;
		$lists['state']	= JHTML::_('grid.state',  $filter_state );
		$lists['search']= $search;
		$model = & $this->getModel();
		foreach($items as $item)
		{
			$item->frontpage=$model->getFontpage($item->product_id);			
		}
		foreach($items as $item)
		{
			$item->status=$model->getNewproduct($item->product_id);
		}
		$this->assignRef('lists',		$lists);
		$this->assignRef('items',		$items);
		$this->assignRef('pagination',	$pagination);
		parent::display($tpl);
	}
	private function getListCategory($id,$text){
		global $category;
		$db =&JFactory::getDBO();
		$query = 'select * from #__category where parent_id= '.$id.' ORDER BY ordering ';
		$db->setQuery($query);
		$rows = $db->loadAssocList();
		foreach($rows as $key=>$value){
			$value['title'] = substr($text.'|_ '.$value['title'],2);
			$category[] = $value;
			$this->getListCategory($value['id'],$text.'. &nbsp;&nbsp;&nbsp;&nbsp;');
		} 
		return $category;
	}
	function createSel($arr,$id){
		if(count($arr)){
			foreach($arr as $key =>$val){
				$parent[] = $val['parent_id'];
			}
			$str = '<select id="parentid" name="parent" class="inputbox" onchange="submitform();">';
			$str.='<option value="0">Select Category</option>';	
			foreach($arr as $key =>$value){				
				if(in_array($value['id'],$parent)){
					$str.='<option value="'.$value['id'].'" disabled="disabled">'.$value['title'].'</option>';	
				}else{
					if($value['id']==$id)
						$str.='<option value="'.$value['id'].'" selected="selected">'.$value['title'].'</option>';	
					else
						$str.='<option value="'.$value['id'].'">'.$value['title'].'</option>';	
				}
			}
			$str.='</select>';
		}
		return $str;
		
	}
}